package com.hcj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcj.mapper.CategoryMapper;
import com.hcj.mapper.ProductMapper;
import com.hcj.pojo.Category;
import com.hcj.pojo.Product;
import com.hcj.service.CategoryService;
import com.hcj.util.MyResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper mapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public MyResponse add(Category category) {
        // 商品类别是否重复
        Category category1 = mapper.selectOne(new LambdaQueryWrapper<Category>()
                .eq(Category::getName, category.getName()));
        if (category1 != null) {
            return new MyResponse("200", "商品类别名已存在");
        }

        int insert = mapper.insert(category);

        if (insert == 1) {
            return new MyResponse("100", "商品类别添加成功!");
        }

        return new MyResponse("200", "商品类别信息插入异常!");
    }

    @Override
    public MyResponse update(Category category) {
        // 根据 id 来对商品信息做更新

        // 判断 category 是否携带要修改的商品类别的id
        Integer id = category.getId();
        if (id == null) {
            return new MyResponse("200", "要修改的商品类别不存在！");
        }

        // 判断 这个 id 的商品类别是否真的存在
        Category category1 = mapper.selectOne(new LambdaQueryWrapper<Category>()
                .eq(Category::getId, category.getId()));

        if (category1 == null) {
            return new MyResponse("200", "要修改的商品类别不存在！");
        }

        // 最后执行更新
        int i = mapper.updateById(category);

        if (i == 1) {
            return new MyResponse("100", "商品类别信息修改成功!");
        }
        return new MyResponse("200", "商品类别信息修改异常!");
    }

    @Override
    public MyResponse delete(String name) {
        // 判断这个名称的商品类别是否真的存在
        Category category = mapper.selectOne(new LambdaQueryWrapper<Category>()
                .eq(Category::getName, name));

        if (category == null) {
            return new MyResponse("200", "要删除的商品类别不存在！");
        }
        // 判断这个商品类别下是否存在商品
        List<Product> productList = productMapper.selectByCategory(name);
        if (!productList.isEmpty()) {
            return new MyResponse("210", "该商品类别下存在商品!");
        }

        // 最后执行删除
        int i = mapper.deleteById(category.getId());

        if (i == 1) {
            return new MyResponse("100", "商品类别信息删除成功!");
        }
        return new MyResponse("200", "商品类别信息删除异常!");
    }

    @Override
    public Page<Category> selectOneByName(String name,Integer currentPage,Integer pageSize) {
        Page<Category> categoryPage = new Page<>();
        categoryPage.setSize(pageSize);
        categoryPage.setCurrent(currentPage);
//        System.out.println("name = " + name);

        LambdaQueryWrapper<Category> like = new LambdaQueryWrapper<Category>()
                .like(Category::getName, "%"+name+"%");
//        System.out.println("like = " + like);

        Page<Category> categoryPage1 = mapper.selectPage(categoryPage, like);
//        System.out.println("categoryPage1 = " + categoryPage1.getSize());
//        System.out.println("categoryPage1.getRecords() = " + categoryPage1.getRecords());


        return categoryPage1;
    }

    @Override
    public Page<Category> selectAllPage(Integer currentPage, Integer pageSize){
        Page<Category> catePage = new Page<>();
        catePage.setSize(pageSize);
        catePage.setCurrent(currentPage);
        return mapper.selectPage(catePage,null);
    }

    //返回所有
    @Override
    public  List<Category> selectAll(){
        return mapper.selectAll();
    }

}
